package com.zbkj.admin.controller.platform;

import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.TransferOrder;
import com.zbkj.common.model.TransferOrderDetails;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.*;
import com.zbkj.common.response.SaleRoomOrWarehouseResponse;
import com.zbkj.common.response.TransferOrderProductAttrValueResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.TransferOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 门店、仓库调货单 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/platform/transfer-order")
@Api(tags = "门店、仓库调货单")
public class TransferOrderController {

    @Autowired
    private TransferOrderService transferOrderService;

    /**
     * 分页显示门店、仓库调货单
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     * @author keNan
     * @since 2025-06-12
     */
    @PreAuthorize("hasAuthority('transfer-order:transfer:list')")
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<TransferOrder>> getList(@Validated TransferOrderSearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<TransferOrder> transferOrderCommonPage = CommonPage.restPage(transferOrderService.getList(request, pageParamRequest));
        return CommonResult.success(transferOrderCommonPage);
    }

    /**
     * 新增门店、仓库调货单
     *
     * @param transferOrderRequest 新增参数
     * @author keNan
     * @since 2025-06-12
     */
    @PreAuthorize("hasAuthority('transfer-order:transfer:save')")
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Valid TransferOrderRequest transferOrderRequest) {
        if (transferOrderService.addTransferOrder(transferOrderRequest)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除门店、仓库调货单
     *
     * @param id Integer
     * @author keNan
     * @since 2025-06-12
     */
    @PreAuthorize("hasAuthority('transfer-order:transfer:delete')")
    @Deprecated
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id) {
        if (transferOrderService.removeById(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改门店、仓库调货单
     *
     * @param id                   integer id
     * @param transferOrderRequest 修改参数
     * @author keNan
     * @since 2025-06-12
     */
    @PreAuthorize("hasAuthority('transfer-order:transfer:update')")
    @Deprecated
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestParam Integer id, @RequestBody @Validated TransferOrderRequest transferOrderRequest) {
        TransferOrder transferOrder = new TransferOrder();
        BeanUtils.copyProperties(transferOrderRequest, transferOrder);
        transferOrder.setId(id);

        if (transferOrderService.updateById(transferOrder)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 查询门店、仓库调货单详情信息
     *
     * @author keNan
     * @since 2025-06-12
     */
    @PreAuthorize("hasAuthority('transfer-order:transfer:info')")
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public CommonResult<CommonPage<TransferOrderDetails>> info(@RequestBody @Valid IdRequest idRequest) {
        PageInfo<TransferOrderDetails> transferOrderDetailsList = transferOrderService.info(idRequest);
        return CommonResult.success(CommonPage.restPage(transferOrderDetailsList));
    }


    @PreAuthorize("hasAuthority('transfer-order:transfer:selectProductAttrValueList')")
    @ApiOperation(value = "新增调货单-查询商品规格")
    @RequestMapping(value = "/selectProductAttrValueList", method = RequestMethod.POST)
    public CommonResult<CommonPage<TransferOrderProductAttrValueResponse>> selectProductAttrValueList(@RequestBody @Valid ProductAttrValueSearchRequest productAttrValueSearchRequest) {
        PageInfo<TransferOrderProductAttrValueResponse> p = transferOrderService.selectProductAttrValueList(productAttrValueSearchRequest);
        return CommonResult.success(CommonPage.restPage(p));
    }


    @PreAuthorize("hasAuthority('transfer-order:transfer:review')")
    @ApiOperation(value = "审核")
    @RequestMapping(value = "/review", method = RequestMethod.POST)
    public CommonResult<Boolean> review(@RequestBody ReviewRequest reviewRequest) {
        Boolean b = transferOrderService.review(reviewRequest);
        return CommonResult.success(b);
    }


    @PreAuthorize("hasAuthority('transfer-order:transfer:listByType')")
    @ApiOperation(value = "根据类型分页查询仓库或者门店")
    @RequestMapping(value = "/listByType", method = RequestMethod.POST)
    public CommonResult<CommonPage<SaleRoomOrWarehouseResponse>> listByType(@RequestBody @Valid TypeRequest typeRequest) {
        PageInfo<SaleRoomOrWarehouseResponse> pageInfo = transferOrderService.listByType(typeRequest);
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }
}



